package Meituan.threadPoolTest;

import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author: leiwenkang
 * @date: 2019/7/16
 * @time: 10:33
 */
public class MainTest {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        for (int i = 0; i < 50; i++) {
            exePar();
//        exeSer();
        }
        System.out.println("主线程执行结束, 耗时: " + (System.currentTimeMillis() - start));
    }

    private static void exeSer() {
        doTask1();
        doTask2();
    }


    private static void exePar() {
        FutureTask<Void> task1 = new FutureTask<>(() -> {
            //构建block返回结果
            doTask1();
            return null;
        });

        ThreadPoolUtils.BLOCK_BANNER_POOL.submit(task1);

        FutureTask<Void> task2 = new FutureTask<>(() -> {
            //构建block返回结果
            doTask2();
            return null;
        });

        ThreadPoolUtils.BLOCK_BANNER_POOL.submit(task2);

        try {
            task1.get(200, TimeUnit.MILLISECONDS);
            System.out.println("task1成功执行");
        } catch (Exception e) {
            System.out.println("task1执行超时");
        }

        try {
            task2.get(200, TimeUnit.MILLISECONDS);
            System.out.println("task2成功执行");
        } catch (Exception e) {
            System.out.println("task2执行超时");
        }

        ThreadPoolUtils.BLOCK_BANNER_POOL.shutdown();

    }



    private static void doTask1() {
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static void doTask2() {
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
